package com.tonann.app.service.impl;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;

import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.sun.javafx.iio.ImageStorage;
import com.tonann.app.minio.MinioService;
import com.tonann.common.config.RuoYiConfig;
import com.tonann.common.utils.DateUtils;
import com.tonann.common.utils.SecurityUtils;
import com.tonann.common.utils.file.FileUploadUtils;
import com.tonann.framework.config.ServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tonann.app.mapper.AppDeviceModelMapper;
import com.tonann.app.domain.AppDeviceModel;
import com.tonann.app.service.IAppDeviceModelService;

import javax.annotation.Resource;
import javax.imageio.ImageIO;

/**
 * 设备型号管理Service业务层处理
 *
 * @author tonann
 * @date 2023-07-31
 */
@Service
public class AppDeviceModelServiceImpl implements IAppDeviceModelService
{
    @Resource
    private AppDeviceModelMapper appDeviceModelMapper;

    @Resource
    private ServerConfig serverConfig;

    @Resource
    private MinioService minioService;
    /**
     * 查询设备型号管理
     *
     * @param modelId 设备型号管理主键
     * @return 设备型号管理
     */
    @Override
    public AppDeviceModel selectAppDeviceModelByModelId(Long modelId)
    {
        return appDeviceModelMapper.selectAppDeviceModelByModelId(modelId);
    }

    /**
     * 查询设备型号管理列表
     *
     * @param appDeviceModel 设备型号管理
     * @return 设备型号管理
     */
    @Override
    public List<AppDeviceModel> selectAppDeviceModelList(AppDeviceModel appDeviceModel)
    {
        return appDeviceModelMapper.selectAppDeviceModelList(appDeviceModel);
    }

    /**
     * 新增设备型号管理
     *
     * @param appDeviceModel 设备型号管理
     * @return 结果
     */
    @Override
    public int insertAppDeviceModel(AppDeviceModel appDeviceModel) throws IOException {

        AppDeviceModel deviceModel = appDeviceModelMapper.selectAppDeviceModelByModelCode(appDeviceModel.getModelCode());
        if (deviceModel != null) {
            return 0;
        }
        String content = "http://59.110.170.159/document?modelCode=" + appDeviceModel.getModelCode();
        QrConfig qrConfig = new QrConfig();
        String url = appDeviceModel.getModelImgUrl();
        if (url != null) {
            BufferedImage bufferedImage = ImageIO.read(new URL(url));
            qrConfig.setImg(bufferedImage);
            qrConfig.setRatio(4);
        }
        try {
            BufferedImage bufferedImage =  QrCodeUtil.generate(content,qrConfig);
            String filePath  = minioService.uploadBufferImage(bufferedImage);
            if (filePath.contains("?")){
                filePath = filePath.substring(0,filePath.indexOf("?"));
            }
            appDeviceModel.setCreateBy(SecurityUtils.getUsername());
            appDeviceModel.setCreateTime(DateUtils.getNowDate());
            appDeviceModel.setUpdateBy(SecurityUtils.getUsername());
            appDeviceModel.setUpdateTime(DateUtils.getNowDate());
            appDeviceModel.setModelQRUrl(filePath);
        }catch (Exception e){
            e.printStackTrace();
        }
        return appDeviceModelMapper.insertAppDeviceModel(appDeviceModel);
    }

    /**
     * 修改设备型号管理
     *
     * @param appDeviceModel 设备型号管理
     * @return 结果
     */
    @Override
    public int updateAppDeviceModel(AppDeviceModel appDeviceModel) throws IOException {
        AppDeviceModel deviceModel = appDeviceModelMapper.selectAppDeviceModelByModelId(appDeviceModel.getModelId());
        if (deviceModel != null && deviceModel.getModelCode() != null && !deviceModel.getModelCode().equals(appDeviceModel.getModelCode())){
            if (appDeviceModelMapper.selectAppDeviceModelByModelCode(appDeviceModel.getModelCode()) != null){
                return  0;
            }
        }else  if (deviceModel != null && deviceModel.getModelCode() != null && deviceModel.getModelQRUrl() == null) {
            String content = "https://www.emodule.com.cn/document?modelCode=" + appDeviceModel.getModelCode();
            // 上传文件路径
            String filePath = RuoYiConfig.getQRPath();
            File dir = new File(filePath);
            if (!dir.exists()) {
                dir.mkdirs();
            }

            QrConfig qrConfig = new QrConfig();
            String url = appDeviceModel.getModelImgUrl();
            if (url != null) {
                BufferedImage bufferedImage = ImageIO.read(new URL(url));
                qrConfig.setImg(bufferedImage);
                qrConfig.setRatio(4);
            }
            String fileName = System.currentTimeMillis() + ".png";
            File file = new File(filePath + "/" + fileName);
            file = QrCodeUtil.generate(content, qrConfig, file);
            fileName = FileUploadUtils.getPathFileName(filePath, fileName);
//            String url = serverConfig.getUrl() + fileName;
            appDeviceModel.setModelQRUrl(fileName);
        }

        appDeviceModel.setUpdateBy(SecurityUtils.getUsername());
        appDeviceModel.setUpdateTime(DateUtils.getNowDate());
        return appDeviceModelMapper.updateAppDeviceModel(appDeviceModel);
    }

    /**
     * 批量删除设备型号管理
     *
     * @param modelIds 需要删除的设备型号管理主键
     * @return 结果
     */
    @Override
    public int deleteAppDeviceModelByModelIds(Long[] modelIds)
    {
        return appDeviceModelMapper.deleteAppDeviceModelByModelIds(modelIds);
    }

    /**
     * 删除设备型号管理信息
     *
     * @param modelId 设备型号管理主键
     * @return 结果
     */
    @Override
    public int deleteAppDeviceModelByModelId(Long modelId)
    {
        return appDeviceModelMapper.deleteAppDeviceModelByModelId(modelId);
    }

    @Override
    public AppDeviceModel selectAppDeviceModelByModelCode(String modelCode) {
        return appDeviceModelMapper.selectAppDeviceModelByModelCode(modelCode);
    }
}
